package com.humngyang.com.hmy.service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.humngyang.com.hmy.entity.Userrole;
import com.humngyang.com.hmy.mapper.UserroleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.UUID;

@Service
public class UserroleService {
    
    @Autowired
    UserroleMapper userroleMapper;
    
    /**
     * Author : hmy
     * @date 2020/2/24 21:05
     * describution : 添加用户和角色的信息
     **/

    public int addUserRole(Userrole userRole){
        if (StringUtils.isEmpty(userRole.getId())){
            userRole.setId(UUID.randomUUID().toString().replace("-",""));
            int insert = userroleMapper.insert(userRole);
            return insert;
        }else {
            int i = userroleMapper.updateById(userRole);
            return i;
        }
    }
    
    /**
     * Author : hmy
     * @date 2020/2/24 23:20
     * describution : 根据用户获取对应的角色名
     **/

    public Userrole getUserRoleByUserId(String id){
        Userrole userRole = userroleMapper.selectOne(new QueryWrapper<Userrole>().eq("userid", id));
        if (userRole != null ){
            return userRole;
        }
        return new Userrole();
    }
}
